package com.qf.homework.day38.controller;


import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.qf.homework.day38.pojo.ResultEntity;
import com.qf.homework.day38.pojo.User;
import com.qf.homework.day38.service.UserService;
import java.io.IOException;

/**
 * 新用户注册
 * @param   * username 用户名
 *            password 密码
 *            address 地址
 *            hobbies 爱好
 * @return  resultEntity:  status 状态码
 *                            msg    返回提示消息
 */
@WebServlet("/Register")
public class UserController extends HttpServlet {
    private UserService userService=new UserService();
    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.setContentType("utf-8");
        resp.setContentType("application/json");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String address = req.getParameter("address");
        String hobbies = req.getParameter("hobbies");
        ResultEntity resultEntity = new ResultEntity();
        System.out.println(username+"|"+password);
        if (username==null||password==null)
        {
            resultEntity.setMsg("用户名或密码为空");
            resultEntity.setStatus(401);
        }
        else {
           User  user = new User(username, password, address, hobbies);
            int code = userService.register(user);
            resultEntity.setStatus(code);
            if (code==403){resultEntity.setMsg("用户已存在");}
            else if (code==500){resultEntity.setMsg("服务器繁忙");}
        }

        resp.getWriter().write(JSONObject.toJSONString(resultEntity));

    }


}
